package com.softsec.isc.analysis;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class GetDexStringId {

	@SuppressWarnings("unused")
	public static void StringId(String fileName) throws IOException{	
		int flag=GetFinal.getFlag(fileName);
		byte[] bufferArray = new byte[4];
	
		byte[] temp = apkExtractor.getDex(fileName);
		int length = temp.length;
		DataInputStream input=apkExtractor.getDexDataInputStreamWithBuffered(fileName);
		input.mark(length);
		input.skipBytes(112);
		int count=DexMapItemMapper.getInstance().getDexMapItemCount("kDexTypeStringIdItem");
		int offset=0;
		for(int i=0;i<count;i++){			
			input.read(bufferArray);
			offset=GetFinal.GetSixteen(bufferArray, flag);			
			DexMapItemMapper.getInstance().addDexMapString(i, offset);			
		}
		
		String MutfString="";
		int StringNum=0;
	//	input = apkExtractor.getDexDataInputStreamWithBuffered(fileName);
		input.reset();
		for(int j=0;j<count;j++){			
			StringBuilder input2=new StringBuilder();
			offset = DexMapItemMapper.getInstance().getDexStringOffset(j);	
			input.mark(length);
			input.skip(offset);
			MutfString="";
			byte read = 0;			
			StringNum=Uleb128.readLeb128(input);		
			while((read=input.readByte())!=0){
				input2.append((char)(read));
			}
			MutfString=input2.toString();
			input2.replace(0, input2.length(), "");
		//	System.out.println(j+" /t "+MutfString);
			DexMapItemMapper.getInstance().addDexStringId(j, MutfString);
			MutfString=null;			
			input.reset();
		}
		
		
		input.close();
		
//		java.util.Calendar c=java.util.Calendar.getInstance();   
//		java.text.SimpleDateFormat f1=new java.text.SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");   
//		System.out.println(f1.format(c.getTime())); 

		
	}
	
	public static boolean GetRegular(String urltest) {
		boolean b=false;
		String url="[a-zA-z]{1,5}://[a-zA-Z0-9_]+((\\.|/)[a-zA-Z0-9_]+)*((#|\\?)[\\s\\S]+)?";
		Pattern pattern=Pattern.compile(url);
		Matcher matcher = pattern.matcher(urltest);
		b= matcher.lookingAt();			
		return b;		
	}

}
